#include "ui/ProjectTreeWidget.h"
#include <QDir>
#include <QFileInfo>

ProjectTreeWidget::ProjectTreeWidget(QWidget *parent) : QTreeWidget(parent) {}

void ProjectTreeWidget::loadProject(const QString &projectPath)
{
    clear();
    QTreeWidgetItem *root = new QTreeWidgetItem(this);
    root->setText(0, QFileInfo(projectPath).fileName());
    addTreeItems(root, projectPath);
    expandAll();
}

void ProjectTreeWidget::addTreeItems(QTreeWidgetItem *parent, const QString &path)
{
    QDir dir(path);
    QFileInfoList list = dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
    
    for (const QFileInfo &info : list) {
        if (info.fileName().startsWith(".")) continue;
        
        QTreeWidgetItem *item = new QTreeWidgetItem(parent);
        item->setText(0, info.fileName());
        item->setData(0, Qt::UserRole, info.absoluteFilePath());
        
        if (info.isDir()) {
            addTreeItems(item, info.absoluteFilePath());
        }
    }
}